package cn.xfdzx.android.apps.shop.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.NearLoactionAdapter;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.LocationInfo;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.evenbus.HomeAdressMessage;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;

import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP;

/**
 * @author gzk
 * @data 2020/03/27
 * 地址搜索页
 */
public class SearchAddressActivity extends BaseMvpActivity implements PoiSearch.OnPoiSearchListener {

    @BindView(R.id.et_address_search)
    EditText etAddressSearch;
    @BindView(R.id.iv_address_search_delete)
    ImageView ivAddressSearchDelete;
    @BindView(R.id.rv_search_key)
    RecyclerView rvSearchKey;
    @BindView(R.id.tv_no_record)
    TextView tvNoRecord;
    @BindView(R.id.ll_state)
    LinearLayout llState;

    private List<LocationInfo> list = new ArrayList<>();
    private NearLoactionAdapter nearLoactionAdapter;
    private RelativeLayout.LayoutParams item_bar;
    private String pageForm="";//判断是否是首页跳转
    @Override
    public int getLayoutId() {
        return R.layout.activity_search_address;
    }

    @Override
    public void initView() {
        initHigh();
        pageForm = getIntent().getStringExtra("pageForm")==null?"":getIntent().getStringExtra("pageForm");
        etAddressSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable .toString().isEmpty()) {
                    ivAddressSearchDelete.setVisibility(View.INVISIBLE);
                    rvSearchKey.setVisibility(View.INVISIBLE);
//                    tvNoRecord.setVisibility(View.VISIBLE);

                } else {
//                    if (tvNoRecord.getVisibility()==View.VISIBLE){
//                        tvNoRecord.setVisibility(View.GONE);
//                    }
                    ivAddressSearchDelete.setVisibility(View.VISIBLE);
                    nearLocation(editable.toString());
                    rvSearchKey.setVisibility(View.VISIBLE);

                }

            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvSearchKey.setLayoutManager(linearLayoutManager);
        nearLoactionAdapter = new NearLoactionAdapter(list, this);
        rvSearchKey.setNestedScrollingEnabled(false);
        rvSearchKey.setAdapter(nearLoactionAdapter);

        nearLoactionAdapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (EmptyUtil.isNotEmpty(pageForm)){
                    EventBus.getDefault().post(new HomeAdressMessage(list.get(position).getTitle(),list.get(position).getLatitude()+"",
                            list.get(position).getLonTitude()+""));
                    Intent intent = new Intent(SearchAddressActivity.this, MainActivity.class);
                    intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
                    intent.addFlags(FLAG_ACTIVITY_SINGLE_TOP);
                    startActivity(intent);
                }else {
                    Intent intent=new Intent();
                    intent.putExtra("address_name",list.get(position).getAddress());
                    intent.putExtra("lontitude",list.get(position).getLonTitude()+"");
                    intent.putExtra("latitude",list.get(position).getLatitude()+"");
                    intent.putExtra("district",list.get(position).getDistrict()+"");
                    intent.putExtra("city",list.get(position).getCity()+"");
                    setResult(RESULT_OK,intent);
                }
                finish();
            }
        });
    }
    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(this);
        llState.setLayoutParams(item_bar);
    }
    @OnClick({R.id.iv_location_back, R.id.tv_address_search_cancel, R.id.iv_address_search_delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_location_back:
            case R.id.tv_address_search_cancel:
                finish();
                break;
            case R.id.iv_address_search_delete:
                etAddressSearch.getText().clear();
                break;
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    private void nearLocation(String name) {
        //不设置POI的类别，默认返回“餐饮服务”、“商务住宅”、“生活服务”这三种类别的POI
        PoiSearch.Query query = new PoiSearch.Query(name, "", "北京");
        query.setPageSize(40);
        query.setPageNum(1);
        PoiSearch search = new PoiSearch(this, query);

        search.setOnPoiSearchListener(this);
        search.searchPOIAsyn();
    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        list.clear();
        ArrayList<PoiItem> pois = poiResult.getPois();
        for (PoiItem poi : pois) {

            LocationInfo info = new LocationInfo();
            LatLonPoint point = poi.getLatLonPoint();

            info.setTitle(poi.getTitle());
            info.setAddress(poi.getSnippet());
            info.setLatitude(point.getLatitude());
            info.setLonTitude(point.getLongitude());
            info.setCity(poi.getCityName());
            info.setDistrict(poi.getAdName());

            list.add(info);

            Log.i("名称", poi.getTitle());
            Log.i("详细地址", info.getAddress());

        }
        nearLoactionAdapter.setList(list);
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }
}
